package cn.wetcland.urban.admin.service.impl;

import cn.wetcland.urban.admin.dto.CustomerUpdateDTO;
import cn.wetcland.urban.admin.mapper.UtCustomerMapper;
import cn.wetcland.urban.admin.service.UtCustomerService;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.utils.bcrypt.BCryptPasswordEncoder;
import cn.wetcland.urban.generator.domain.UtCustomer;
import cn.wetcland.urban.generator.domain.UtDriver;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author Wetcland
* @description 针对表【ut_customer】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtCustomerServiceImpl extends ServiceImpl<UtCustomerMapper, UtCustomer> implements UtCustomerService {

    @Resource
    private UtCustomerMapper customerMapper;

    @Override
    public Page<UtCustomer> customerList(BaseConditionSearchDTO searchDTO) {
        Page<UtCustomer> customerPage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return customerMapper.selectCustomerListPage(customerPage, searchDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleCustomerBanOrUnban(Long customerId) {
        UtCustomer customer = customerMapper.selectById(customerId);
        MyAssert.notNull(customer, "该用户不存在");
        customer.setIsBan(customer.getIsBan()==1?0:1);
        customerMapper.updateById(customer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleCustomerDelete(Long customerId) {
        customerMapper.deleteById(customerId);
    }

    @Override
    public void handleCustomerUpdate(CustomerUpdateDTO updateDTO) {
        UtCustomer customer = customerMapper.selectById(updateDTO.getCustomerId());
        MyAssert.notNull(customer, "该用户不存在");
        customer.setPhone(updateDTO.getPhone());
        if (updateDTO.getPassword() != null && !updateDTO.getPassword().isEmpty()) {
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(updateDTO.getPassword());
            customer.setCustomerPassword(encode);
        }
        customerMapper.updateById(customer);
    }

}




